package com.czl.module_storehouse.activity.baseInfo;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import com.czl.base.data.bean.tengyun.ProductBean;
import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.base.data.bean.tengyun.SortBeanKt;
import com.czl.base.util.SpHelper;
import com.czl.module_base.activity.BaseActivity;
import com.czl.module_base.http.HttpResponse;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_base.mvp.view.SimpleView;
import com.czl.module_base.utils.GlideUtils;
import com.czl.module_service.event.StorehouseHomeScanBackEvent;
import com.czl.module_service.event.StorehouseHomeScanEvent;
import com.czl.module_service.system.Constants;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.activity.allocate.add.AllocateAddActivity;
import com.czl.module_storehouse.activity.borrow.add.BorrowAddActivity;
import com.czl.module_storehouse.activity.receive.add.ReceiveAddActivity;
import com.czl.module_storehouse.activity.scan.ScanActivity;
import com.czl.module_storehouse.activity.shift.add.ShiftAddActivity;
import com.czl.module_storehouse.databinding.ActivityWareSingleProductBinding;
import com.czl.module_storehouse.mvp.presenter.ProductPresenter;
import com.czl.module_storehouse.utils.QrCodeRuleUtils;
import com.jeremyliao.liveeventbus.LiveEventBus;

/**
 * @author xubin
 */
public class WareSingleProductActivity extends BaseActivity<ActivityWareSingleProductBinding> implements SimpleView {

    private ProductBean mProductBean;

    @InjectPresenter
    ProductPresenter mProductPresenter;

    @Override
    protected ActivityWareSingleProductBinding getBinding(LayoutInflater layoutInflater) {
        return ActivityWareSingleProductBinding.inflate(layoutInflater);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mToolBinding.toolbarContentTitle.setText("物品详情");
        binding.textReceive.setOnClickListener(v -> startReceiveActivity());
        binding.textBorrow.setOnClickListener(v -> startBorrowActivity());
        binding.textAllocate.setOnClickListener(v -> startAllocateActivity());
        binding.textShift.setOnClickListener(v -> startShiftActivity());
        LiveEventBus.get(StorehouseHomeScanBackEvent.class).observe(this, storehouseHomeScanBackEvent -> finish());
    }

    @Override
    protected void loadData() {
        super.loadData();
        mProductPresenter.getProductInfoByCode(QrCodeRuleUtils.handleStorehouseProductResult(getIntent().getStringExtra(ScanActivity.RESULT)));
    }

    @Override
    public <T> void showHttpResponse(HttpResponse<T> httpResponse) {
        super.showHttpResponse(httpResponse);
        Object data = httpResponse.getData();
        if (data == null) {
            return;
        }
        if (data instanceof ProductBean) {
            setupView((ProductBean) data);
        }
    }

    private void setupView(ProductBean bean) {
        mProductBean = bean;
        binding.tvTitle.setText(bean.getProductName());
        binding.tvCode.setText(bean.getProductCode());
        binding.tvCompany.setText(bean.getCompanyName());
        binding.tvSort.setText(bean.getClassificationName());
        binding.tvModel.setText(bean.getProductModel());
        binding.tvUnit.setText(bean.getUnit());
        binding.tvPrice.setText(bean.getPrice());
        binding.tvSupplier.setText(bean.getSupplier());
        binding.tvDate.setText(bean.getManufactureDate());
        binding.tvManufacturer.setText(bean.getManufacturer());
        binding.tvLoc.setText(bean.getStorehouseLocatName());
        GlideUtils.loadImage(getContext(), binding.ivImage, bean.getSortImg(), R.mipmap.pic_default_loading, R.mipmap.pic_default_error);
        binding.tvLoc.setText(bean.getStorehouseLocatName());
        if (bean.getStorehouseId() != null && SpHelper.INSTANCE.decodeInt(Constants.SpKey.STOREHOUSE_ID) == bean.getStorehouseId()) {
            binding.llTop.setVisibility(View.VISIBLE);
            binding.llBottom.setVisibility(View.VISIBLE);
            binding.textPrompt.setVisibility(View.GONE);
        } else {
            binding.llTop.setVisibility(View.GONE);
            binding.llBottom.setVisibility(View.GONE);
            binding.textPrompt.setVisibility(View.VISIBLE);
        }
    }

    private void startReceiveActivity() {
        SortBean sortBean = createProduct();
        if (sortBean != null) {
            startActivity(new Intent(getContext(), ReceiveAddActivity.class));
        }
    }

    private void startBorrowActivity() {
        if (createProduct() != null) {
            startActivity(new Intent(getContext(), BorrowAddActivity.class));
        }
    }

    private void startAllocateActivity() {
        if (createProduct() != null) {
            startActivity(new Intent(getContext(), AllocateAddActivity.class));
        }
    }

    private void startShiftActivity() {
        if (createProduct() != null) {
            startActivity(new Intent(getContext(), ShiftAddActivity.class));
        }
    }

    private SortBean createProduct() {
        if (mProductBean == null) {
            return null;
        }
        if (mProductBean.getStorehouseLocatId() == null || mProductBean.getStorehouseLocatId() == 0) {
            showToast("该物品不存在");
            return null;
        }
        SortBean sortBean = SortBeanKt.createProductSortBean(mProductBean);
        LiveEventBus.get(StorehouseHomeScanEvent.class).postDelay(new StorehouseHomeScanEvent(sortBean), 200);
        return sortBean;
    }
}

